<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>01-引出生命周期</title>
</head>
</head>

<body>
    <div id="root"></div>
    <!-- react核心组件库 -->
    <script src="../其他/js/react.development.js"></script>
    <script src="../其他/js/react-dom.development.js"></script>
    <!-- Don't use this in production: -->
    <script src="../其他/js/babel.min.js"></script>

    <!-- 此处一定要写babel -->
    <script type="text/babel">
      class Life extends React.Component {
        // 状态
        state = {opacity: 1}
        death = () => {
          // 卸载组件
          ReactDOM.unmountComponentAtNode(document.getElementById('root'))
        }
        // 组件挂载完毕调用一次
        componentDidMount() {
          this.timer = setInterval(() => {
            let {opacity} = this.state
            opacity -= 0.1
            if(opacity <= 0) opacity = 1
            this.setState({opacity})
          }, 200)
        }
        // 组件将要被卸载了调用
        componentWillUnmount() {
          clearInterval(this.timer)
        }
        render() {
          return (
            <div>
              <h2 style={{opacity: this.state.opacity}}>React 好难学</h2>
              <button onClick={this.death}>不活了</button>
            </div>
          )
        }
      }
      ReactDOM.render(<Life/>,document.getElementById('root'))

    </script>
</body>

</html>